#!/usr/bin/perl
use strict;
use modules::encode;
package csdrop;

sub csdrop {
    my ($dbh,$remote,$input,$csnum) = @_;

    my $sth = $dbh->prepare("SELECT auth FROM ninfo WHERE num = '$$input[0]' AND code = '". encode::encode($$input[0])."';");
    $sth->execute;
    my $auth = $sth->fetchrow_array;

    if ($$input[4] eq "") {
	print $remote "$csnum O $$input[0] :Syntax :DROP <\#Channel>\n";
    }
    elsif ($auth eq "0") {
	print $remote "$csnum O $$input[0] :You have to identify with NickName Services before using this command.\n";
    }
    else {
	$sth=$dbh->prepare("SELECT owner FROM channels WHERE owner = '$auth' AND channel = '$$input[4]';");
	$sth->execute;
	my $owner = $sth->fetchrow_array;
	if (!$owner) {
	    print $remote "$csnum O $$input[0] :You must be the owner of $$input[4] in order to perform the drop command.\n";
	}
	else {
	    print $remote "$csnum O $$input[0] :Unregistering $$input[4] ..\n";
	    $sth=$dbh->prepare("DELETE FROM channels where channel = '$$input[4]';");
	    $sth->execute;
	    $sth=$dbh->prepare("DELETE FROM users WHERE channel = '$$input[4]';");
	    $sth->execute;
	    print $remote "$csnum O $$input[0] :\cB$$input[4] has been unregistered.\cB Channel records have been deleted.\n";
	    print $remote "$csnum L $$input[4]\n";
	}
    }
}
1;
